package com.example.conditional;

import com.example.conditional.condition.LinuxCondition;
import com.example.conditional.condition.WindowsCondition;
import com.example.conditional.service.INameService;
import com.example.conditional.service.impl.LinuxServiceImpl;
import com.example.conditional.service.impl.WindowsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;

@SpringBootApplication
public class ConditionalApplication implements ApplicationRunner {

    @Bean
    @Conditional(WindowsCondition.class)
    public INameService windowsService() {
        return new WindowsServiceImpl();
    }

    @Bean
    @Conditional(LinuxCondition.class)
    public INameService linuxService() {
        return new LinuxServiceImpl();
    }

    public static void main(String[] args) {
        SpringApplication.run(ConditionalApplication.class, args);
    }

    @Autowired
    private INameService nameService;

    @Value("#{systemProperties['os.name']}")
    private String osName;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("参数显示为：" + osName + "创建的 Bean 为：" + nameService.showName());
    }
}
